<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Cookie调试工具</title>
    <style>
        body { font-family: Arial, sans-serif; padding: 20px; }
        .log { background: #f5f5f5; padding: 10px; margin: 10px 0; border-radius: 5px; }
        .error { background: #ffebee; color: #c62828; }
        .success { background: #e8f5e8; color: #2e7d32; }
        button { padding: 10px 20px; margin: 5px; background: #2196f3; color: white; border: none; border-radius: 5px; cursor: pointer; }
        button:hover { background: #1976d2; }
    </style>
</head>
<body>
    <h1>🍪 Cookie调试工具</h1>
    <p>这个工具帮助诊断cookie获取问题</p>
    
    <button onclick="testAllCookies()">测试获取所有Cookie</button>
    <button onclick="testYouTubeCookies()">测试YouTube Cookie</button>
    <button onclick="testPermissions()">测试权限</button>
    <button onclick="clearLog()">清空日志</button>
    
    <div id="log"></div>
    
    <script>
        function log(message, type = 'log') {
            const logDiv = document.getElementById('log');
            const div = document.createElement('div');
            div.className = `log ${type}`;
            div.textContent = `[${new Date().toLocaleTimeString()}] ${message}`;
            logDiv.appendChild(div);
            console.log(message);
        }
        
        function clearLog() {
            document.getElementById('log').innerHTML = '';
        }
        
        function testAllCookies() {
            log('开始测试获取所有Cookie...');
            chrome.cookies.getAll({}, function(cookies) {
                if (chrome.runtime.lastError) {
                    log('获取Cookie失败: ' + chrome.runtime.lastError.message, 'error');
                } else {
                    log(`成功获取到 ${cookies.length} 个Cookie`, 'success');
                    
                    // 显示前10个cookie的详细信息
                    cookies.slice(0, 10).forEach((cookie, index) => {
                        log(`Cookie ${index + 1}: ${cookie.name} = ${cookie.value.substring(0, 50)}... (domain: ${cookie.domain})`);
                    });
                    
                    if (cookies.length > 10) {
                        log(`... 还有 ${cookies.length - 10} 个Cookie`);
                    }
                }
            });
        }
        
        function testYouTubeCookies() {
            log('开始测试YouTube相关Cookie...');
            chrome.cookies.getAll({}, function(allCookies) {
                if (chrome.runtime.lastError) {
                    log('获取Cookie失败: ' + chrome.runtime.lastError.message, 'error');
                    return;
                }
                
                const youtubeCookies = allCookies.filter(cookie => {
                    return cookie.domain.includes('youtube.com') || 
                           cookie.domain.includes('youtube-nocookie.com') ||
                           cookie.domain.includes('google.com') ||
                           cookie.domain.includes('googleapis.com');
                });
                
                log(`找到 ${youtubeCookies.length} 个YouTube相关Cookie`, youtubeCookies.length > 0 ? 'success' : 'error');
                
                youtubeCookies.forEach((cookie, index) => {
                    log(`YouTube Cookie ${index + 1}: ${cookie.name} (domain: ${cookie.domain})`);
                });
            });
        }
        
        function testPermissions() {
            log('开始测试权限...');
            
            // 测试cookies权限
            chrome.cookies.getAll({}, function(cookies) {
                if (chrome.runtime.lastError) {
                    log('cookies权限测试失败: ' + chrome.runtime.lastError.message, 'error');
                } else {
                    log('cookies权限测试通过', 'success');
                }
            });
            
            // 测试activeTab权限
            chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
                if (chrome.runtime.lastError) {
                    log('activeTab权限测试失败: ' + chrome.runtime.lastError.message, 'error');
                } else {
                    log('activeTab权限测试通过', 'success');
                    if (tabs[0]) {
                        log(`当前页面: ${tabs[0].url}`);
                    }
                }
            });
            
            // 测试downloads权限
            log('downloads权限需要在实际下载时测试');
        }
    </script>
</body>
</html>
